package com.service.sale;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import com.dao.sale.SkuItemDao;
import com.dao.sys.MmDao;
import com.model.sale.SkuItemModel;
import com.model.sale.VO.SaleSkuMMVO;
import com.model.sys.MmModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.sale.SkuMasterDao;
import com.model.sale.SkuMasterModel;

@Service
public class SkuMasterService {
	static final Logger logger = LoggerFactory.getLogger(SkuMasterService.class);

	@Autowired
	SkuMasterDao skuMasterDao;

	@Autowired
	SkuItemDao skuItemDao;

	@Autowired
	MmDao sysMmDao;

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectList(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<SkuMasterModel> list = skuMasterDao.selectList(queryMap);
		int count = skuMasterDao.selectListCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 列表查询
	 *
	 * @param queryMap
	 * @return
	 */
	public HttpResult selectListWithWeight(Map<String, Object> queryMap) {
		HttpResult httpResult = new HttpResult();
		EasyuiUtils.addPageNum(queryMap);
		EasyuiUtils.addFilter(queryMap);
		List<Map> list = skuMasterDao.selectListWithWeight(queryMap);
		int count = skuMasterDao.selectListWithWeightCount(queryMap);
		httpResult.setRows(list);
		httpResult.setTotal(count);
		logger.info("selectList:{}", list);
		return httpResult;
	}

	/**
	 * 详情
	 *
	 * @param
	 * @return
	 */
	public HttpResult selectInfor(String skuNum, String skuFactory) {
		HttpResult httpResult = new HttpResult();
		SaleSkuMMVO skuMasterModel = skuMasterDao.load(skuNum, skuFactory);
		if (skuMasterModel == null) {
			return HttpResult.error("sku不存在!");
		}
		// 取得行项目
		Map<String, Object> param = new HashMap<>();
		param.put("skuNum", skuNum);
		param.put("skuFactory", skuFactory);
		System.out.println("参数==" + param);
		List<SkuItemModel> skuItems = skuItemDao.selectList(param);

		skuMasterModel.setSkuItems(skuItems);
		// 取得物料规格
		String mmCode = "";
		for (SkuItemModel skuItemModel : skuItems) {
			if ("N".equals(skuItemModel.getItemType())) {
				mmCode = skuItemModel.getItemNum();
				break;
			}
		}
		MmModel mmInfo = sysMmDao.load(mmCode);
		if (null != mmInfo) {
			skuMasterModel.setNedMmMain(StringUtils.isNotNull(mmInfo.getNedMmMain()) ? mmInfo.getNedMmMain() : "");
		}
		httpResult.setData(skuMasterModel);
		httpResult.setCode(HttpStatus.SUCCESS_CODE);
		return httpResult;
	}

	/**
	 * 保存
	 * 
	 * @param
	 * @return
	 */
	/*
	 * public HttpResult save(SkuMasterModel skuMasterModel) { if (skuMasterModel ==
	 * null) { return HttpResult.error("参数不能为空"); } HttpResult httpResult = new
	 * HttpResult(); int result = 0; if
	 * (!StringUtils.isNotNull(skuMasterModel.getSkuNum())) {
	 * skuMasterModel.setSkuNum(UUID.randomUUID().toString()); result =
	 * skuMasterDao.insert(skuMasterModel); } else { result =
	 * skuMasterDao.update(skuMasterModel); } if (result > 0) {
	 * httpResult.setData(skuMasterModel); } else {
	 * httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
	 * httpResult.setMsg("保存失败!"); } return httpResult; }
	 */

	/**
	 * 删除
	 *
	 * @param
	 * @return
	 */
	public HttpResult delete(String skuId) {
		HttpResult httpResult = new HttpResult();
		if (!StringUtils.isNotNull(skuId)) {
			return HttpResult.error("参数不能为空");
		}
		int result = skuMasterDao.delete(skuId);
		if (result > 0) {
			httpResult.setMsg("删除成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}

	/**
	 * 导出
	 *
	 * @param queryMap
	 * @param response
	 */
	public void export(Map<String, Object> queryMap, HttpServletResponse response) {
		List<SkuMasterModel> list = skuMasterDao.selectList(queryMap);
		logger.info("export:{}", list);
		try {
			EasyExcel.write(response.getOutputStream(), SkuMasterModel.class).sheet("模板").doWrite(list);
		} catch (IOException e) {
			logger.error("导出错误:{}", e.getMessage());
		}

	}

	/**
	 * 冻结SKU
	 *
	 * @param
	 * @return
	 */
	public HttpResult freeze(String skuNum) {
		HttpResult httpResult = new HttpResult();
		if (!StringUtils.isNotNull(skuNum)) {
			return HttpResult.error("参数不能为空");
		}
		// SKU表冻结
		int result = skuMasterDao.freeze(skuNum);
		// 物料表冻结
		sysMmDao.freeze(skuNum);
		if (result > 0) {
			httpResult.setMsg("冻结成功！");
		} else {
			httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
			httpResult.setMsg("不存在此条数据！");
		}
		return httpResult;
	}
}
